package edu.isquen.core.form.dao;

import java.sql.Statement;

import com.mysql.jdbc.Connection;

import edu.isquen.core.base.dao.BaseDAO;
import edu.isquen.core.datasource.model.DataSource;
import edu.isquen.core.datasource.service.DataSourceService;
import edu.isquen.core.form.model.Form;
import edu.isquen.core.form.service.FormService;
import edu.isquen.util.factory.ServiceFactory;

/****************************************************
* 文件名：FormDAO.java
* 文件描述：网络通讯函数集
* 创建人： isquen, 2011 年6 月10 日
* 版本号：1.0
* 修改记录：
****************************************************/
public class FormDAO extends BaseDAO<Form> implements IFormDAO {
	public FormDAO(String clazzName) {
		super(clazzName);
	}

	public void create(Object obj) throws Exception {
		Form form = (Form) obj;
		createDynaTable(form);
		super.create(obj);
	}
	
	public void update(Object obj) throws Exception {
		Form form = (Form) obj;
		updateDynaTable(form);
		super.update(obj);
	}
	
	public void updateDynaTable(Form form){
		
	}
	
	/**********************************************
	* 函数名：createDynaTable
	* 参数：Form
	* 功能描述:创建动态表
	* 返回值：void
	* 抛出异常：Exception
	* 作者：isquen 2011/6/10
	**********************************************/
	public void createDynaTable(Form form) throws Exception{
		Connection con = null;
		Statement statement = null;
		try {
			DataSourceService dtProcess = (DataSourceService) ServiceFactory.createProcess(DataSourceService.class);
			DataSource dt = (DataSource) dtProcess.doView(form.getDataSourceId());
			if (form.getName() != null && !form.getName().equals("")) {
				String sql = "";
				//String sql1="";
				SQLBuilder sqlBuilder = new SQLBuilder(form);
				//sqlBuilder.buildDropTableSql();
				//sql1=sqlBuilder.getSQL();
				sqlBuilder.buildCreateTableSql();
				sql = sqlBuilder.getSQL();
				con = (Connection) dt.getConnection();
				statement = con.createStatement();
				//statement.executeUpdate(sql1);
				statement.executeUpdate(sql);
			}
			
		} catch (Exception e) {
			
		}finally{
			if(statement!=null){
				statement.close();
			}
		}
	}
	
	public void remove(String formid) throws Exception{
		Connection con = null;
		Statement statement = null;
		String sql = "";
		try {
			FormService formService=(FormService)ServiceFactory.createProcess(FormService.class);
			Form form=(Form)formService.doView(formid);
			DataSourceService dtProcess = (DataSourceService) ServiceFactory.createProcess(DataSourceService.class);
			DataSource dt = (DataSource) dtProcess.doView(form.getDataSourceId());
			super.remove(formid);
			if (form.getName() != null && !form.getName().equals("")) {
				SQLBuilder sqlBuilder = new SQLBuilder(form);
				sqlBuilder.buildDropTableSql();
				sql = sqlBuilder.getSQL();
				con = (Connection) dt.getConnection();
				statement = con.createStatement();
				statement.executeUpdate(sql);
			}
			
		} catch (Exception e) {
			
		}finally{
			if(statement!=null){
				statement.close();
			}
		}
	}
}
